{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "IY0bHc5lF46I",
   "metadata": {
    "id": "IY0bHc5lF46I"
   },
   "source": [
    "# 🤖 Qwen Model Agent\n",
    "This notebook runs a chatbot agent using the `Qwen/Qwen1.5-1.8B` model via the `transformers` library in Google Colab.\n",
    "\n",
    "✅ Lightweight model (1.8B) so it runs on Colab Free.\n",
    "⚙️ No extra setup needed beyond `transformers` and `torch`.\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "k37CX_LeeY_1",
   "metadata": {
    "id": "k37CX_LeeY_1"
   },
   "source": [
    "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Dhivya-Bharathy/PraisonAI/blob/main/examples/cookbooks/qwen_colab_agent.ipynb)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "r8_iKyw_cbIx",
   "metadata": {
    "id": "r8_iKyw_cbIx"
   },
   "source": [
    "# Install required libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "qOiX2hqlF46K",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "qOiX2hqlF46K",
    "outputId": "c9781e74-cb79-4512-87ab-4805a14b240e"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m363.4/363.4 MB\u001b[0m \u001b[31m2.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m13.8/13.8 MB\u001b[0m \u001b[31m36.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m24.6/24.6 MB\u001b[0m \u001b[31m31.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m883.7/883.7 kB\u001b[0m \u001b[31m23.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m664.8/664.8 MB\u001b[0m \u001b[31m1.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m211.5/211.5 MB\u001b[0m \u001b[31m2.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m56.3/56.3 MB\u001b[0m \u001b[31m15.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m127.9/127.9 MB\u001b[0m \u001b[31m7.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m207.5/207.5 MB\u001b[0m \u001b[31m4.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m21.1/21.1 MB\u001b[0m \u001b[31m81.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25h"
     ]
    }
   ],
   "source": [
    "\n",
    "!pip install -q transformers accelerate torch\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "PvTxUutEce5s",
   "metadata": {
    "id": "PvTxUutEce5s"
   },
   "source": [
    "# ✅ Use the official BERT Large model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "q898nxgyF46L",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 177,
     "referenced_widgets": [
      "7dfb60f8868d49d08a38cafd31f9801b",
      "d59ec5856d294b1aa35e543b4e5cb542",
      "1457a5a7e1774b8a9b857d5612d17dab",
      "deb60e57aa994fc9b35f3cfdc1ccc773",
      "52a33ecf50024111b7cd38aec15e7a24",
      "a187d524446c4d2384e2bf1f3e95e7f4",
      "3c294b94c6494727a202771ced455cc5",
      "6265eaa200c74ad1a71cf0d9687d96b5",
      "f95ef373d5bf47659081f7dd5451da10",
      "8a7e9defb4564e3e8bd81fa8c64a511b",
      "0ec0f49489e042bb9979d1908ddd88bc",
      "d264ba22bcd247fdbfcfc920d525769b",
      "63246722cf6f4ff2a86077dd81cf6fe1",
      "300a704a527f40e4ac1aa42b3cfe73de",
      "697ad21a2440459ba2fe58970018ab36",
      "f473043a0c174c72a7e370bef9446d64",
      "85e681d7a00344df96dbbae0f6959822",
      "e4ae9e23561642a7a9b637a573e15638",
      "6acca0b962a745cb8801005757dd5814",
      "a785e8ee00ba47c3b267cbf9af23c308",
      "cf28774ceb87469687299e358fbcd458",
      "ee1fff0b490d40dd859dbced838983b1",
      "c40fd1a2e60a42b686e33a4476dc9100",
      "a798a3704a234a65a124050666b6b990",
      "d16fa1087f6747e0a1618bb269a2680b",
      "cca65a59559d4440a210d0b402a70656",
      "d0a59c8d2bc048d4bbec5c9ab76b387b",
      "a45c0d091c334f60b81d95ecee27ec7d",
      "70680996ad7743c8bcf732f6fad29f11",
      "b5ca16637f4e41ecbdbceb7cb7efa9dd",
      "9ce4a0d49355444c927cea148e095581",
      "5c0cb655347246548e97187512083ef6",
      "2cf395b2a0ab4fcf9679c5a2063b138e",
      "d41037046f8f43ccb008e6796a107a18",
      "304646ee1ed74c03b4962628a80618b9",
      "812518655bc04923972b18a6963e4c07",
      "f814bc25369e46d6b5a1ee0717f95bdb",
      "4e6e66ce7ca54447871899683f80cf05",
      "eccca4ade2404c84b787ffe45e625e99",
      "a1c746f57c9b4dacba349257bd3598ad",
      "acbe0aff4b6249c28636cdcf97445a28",
      "fdee9e7c52de42278364b228cb1fdda2",
      "f59346d4b3ce4e5896f4ec2b5600fd9a",
      "177feb0a5c9340ce8d8b8ca7f1539bf0",
      "d33ea2cd6c964b0da990c11fcc811359",
      "0a60b58528a8444fab2a1551f430a9ac",
      "dc11385d525445bd972c4de432de0d6a",
      "7bd7dc33413f4f3b86214a52d68db591",
      "fe3b7d8f9f514666b851771f77cfb1e7",
      "7d4d2c277a8746c7aebfbaaf21b59734",
      "33f354ddd9f54e02975dde2d0631ae04",
      "85ae7ced25884771a0a3f0b9871190e5",
      "a6db2526ea5a4c519ad2e4276b6f2e80",
      "fc771570af3644cc86a5305642cdb42b",
      "0964039f89604e7bbdc168fbddbfe620"
     ]
    },
    "id": "q898nxgyF46L",
    "outputId": "144b0e69-680e-4633-8875-a3e23cccbcc1"
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7dfb60f8868d49d08a38cafd31f9801b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "config.json:   0%|          | 0.00/762 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d264ba22bcd247fdbfcfc920d525769b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "model.safetensors:   0%|          | 0.00/1.34G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c40fd1a2e60a42b686e33a4476dc9100",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "tokenizer_config.json:   0%|          | 0.00/49.0 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d41037046f8f43ccb008e6796a107a18",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "vocab.txt:   0%|          | 0.00/213k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d33ea2cd6c964b0da990c11fcc811359",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "tokenizer.json:   0%|          | 0.00/436k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from transformers import AutoTokenizer, AutoModel\n",
    "import torch\n",
    "\n",
    "bert_model = AutoModel.from_pretrained(\"bert-large-cased\")\n",
    "bert_tokenizer = AutoTokenizer.from_pretrained(\"bert-large-cased\")\n",
    "\n",
    "def classify_text(text):\n",
    "    inputs = bert_tokenizer(text, return_tensors=\"pt\")\n",
    "    with torch.no_grad():\n",
    "        outputs = bert_model(**inputs)\n",
    "    return outputs.last_hidden_state.mean(dim=1)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "jCvfJT7pcg7P",
   "metadata": {
    "id": "jCvfJT7pcg7P"
   },
   "source": [
    "# Agent Function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "RGVb7FBdF46L",
   "metadata": {
    "id": "RGVb7FBdF46L"
   },
   "outputs": [],
   "source": [
    "\n",
    "def chat_with_qwen(prompt: str, max_length: int = 256):\n",
    "    inputs = tokenizer(prompt, return_tensors=\"pt\").to(model.device)\n",
    "    with torch.no_grad():\n",
    "        outputs = model.generate(**inputs, max_new_tokens=max_length)\n",
    "    return tokenizer.decode(outputs[0], skip_special_tokens=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "JRx9L-oYcixp",
   "metadata": {
    "id": "JRx9L-oYcixp"
   },
   "source": [
    "# Example prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0atGDbNAF46M",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "0atGDbNAF46M",
    "outputId": "8878d6fe-aff7-4e9d-d359-e1f219c4dc47"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:151643 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "What are the benefits of using transformers library? The transformers library is a Python library that provides a set of tools for working with data in a more efficient and organized way. Here are some of the benefits of using the transformers library:\n",
      "\n",
      "1. Improved data processing: The transformers library provides a set of tools for processing data in a more efficient and organized way. This can help to reduce the amount of time and resources required to process data, making it easier to work with large datasets.\n",
      "2. Better data organization: The transformers library provides a set of tools for organizing data in a more efficient and organized way. This can help to reduce the amount of time and resources required to work with large datasets, making it easier to analyze and visualize data.\n",
      "3. Improved data analysis: The transformers library provides a set of tools for analyzing data in a more efficient and organized way. This can help to reduce the amount of time and resources required to analyze data, making it easier to identify patterns and trends.\n",
      "4. Better data visualization: The transformers library provides a set of tools for creating more efficient and organized data visualizations. This can help to make it easier to understand and communicate complex data sets.\n",
      "5. Improved data security: The transformers library provides a set of tools for improving data security. This can help to reduce the risk of data\n"
     ]
    }
   ],
   "source": [
    "\n",
    "response = chat_with_qwen(\"What are the benefits of using transformers library?\")\n",
    "print(response)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
